import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

public class Heap {
    private int[] elem;
    private int UseSize;

    public Heap(){
        this.elem = new int[10];
    }

    public void initHeap(int[] array){
        for (int i = 0; i < array.length; i++) {
            elem[i] = array[i];
            this.UseSize++;
        }
    }

    private void swap(int[] elem,int i,int j){
        int tmp = elem[i];
        elem[i] = elem[j];
        elem[j] = tmp;
    }

    public void creatHeap(){
        //每个子树都要进行调整，意味着每个parent都要经过
        for (int parent = (this.UseSize - 1 - 1)/2; parent >= 0; parent--) {
            shiftDown(parent,this.UseSize);
        }
    }

    private void shiftDown(int parent,int UseSize) {
        int child = parent * 2 + 1;
        while (child < UseSize) {
            //找左右孩子哪个大
            if (child+1 < UseSize && elem[child] < elem[child + 1]) {
                child++;
            }
            //孩子大进行交换
            if(elem[child] > elem[parent]){
                swap(elem,parent,child);
                parent = child;
                child = parent*2 + 1;
            }else{
                break;
            }
        }
    }


    //对堆进行删除元素的操作
    public int poll(){
        if(isEmpty()){
            return -1;
        }
        int val = elem[0];
        swap(elem,0,this.UseSize - 1);
        shiftDown(0,this.UseSize - 1);
        return val;
    }


   //添加元素到堆
    public void offer(int n){
        //满了，二倍扩容
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem,elem.length*2);
        }
        this.elem[this.UseSize++] = n;
        shiftUp(this.UseSize - 1);
    }

    private void shiftUp(int child){
        int parent = (child - 1)/2;
        while(child > 0){
            if(elem[child] > elem[parent]){
                swap(elem,parent,child);
                child = parent;
                parent = (child - 1)/2;
            }else{
                break;
            }
        }
    }

    private boolean isFull(){
        return this.UseSize == this.elem.length;
    }

    private boolean isEmpty(){
        return this.UseSize == 0;
    }


    //堆排序:获得降序，建小堆，反之想要升序，建大堆
    //大堆，升序举例
    public void heapsort(){
        int end = this.UseSize - 1;
        while(end > 0){
            swap(elem,0,end);
            shiftDown(0,end );
            end--;
        }
    }

   //topk问题解决
    // 思路一
    public int[] topk1(int[] arr,int k){
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        int[] ret = new int[k];
        for (int i = 0; i < arr.length; i++) {
            priorityQueue.offer(arr[i]);
        }

        for (int i = 0; i < k; i++) {
            ret[i] = priorityQueue.poll();
        }


        return ret;
    }

    //思路二
    class IntCmp implements Comparator<Integer>{
        @Override
        public int compare(Integer o1, Integer o2) {
            return o2.compareTo(o1);
        }
    }
    public int[] topk2(int[] arr,int k){
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new IntCmp());
        int[] ret = new int[k];
        //前k个进入堆
        for (int i = 0; i < k; i++) {
            priorityQueue.offer(arr[i]);
        }

        //遍历剩下的数据，与进行比较
        for (int i = k; i < arr.length; i++) {
            int peekVal = priorityQueue.peek();
            if(arr[i] < peekVal){
                priorityQueue.poll();
                priorityQueue.offer(arr[i]);
            }
        }

        //放到数组里
        for (int i = 0; i < k; i++) {
            ret[i] = priorityQueue.poll();
        }



        return ret;
    }
}